package com.ellecity06.cwhiskymall.view.dialog

import android.app.Activity
import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import androidx.core.text.buildSpannedString
import androidx.core.text.color
import com.ellecity06.common.base.BaseDialog
import com.ellecity06.common.ktx.colorResToColorId
import com.ellecity06.cwhiskymall.R
import com.ellecity06.cwhiskymall.constant.BusinessConstans
import com.ellecity06.cwhiskymall.databinding.DialogReceiveConfirmBinding

/**
 * @author ellecity06
 * @time 2024/10/30 14:16
 * @des
 */
class ReceiveConfirmDialog(
    val context: Activity,
    private val amount: String,
    val onReceive: () -> Unit,
    val onToHome: () -> Unit
) : BaseDialog<DialogReceiveConfirmBinding>(context), View.OnClickListener {
    override val widthP: Float
        get() = 1F

    override fun inflaterViewBinding(): DialogReceiveConfirmBinding {
        return DialogReceiveConfirmBinding.inflate(LayoutInflater.from(context))
    }

    override fun initialize() {
        binding.tvContent.text = buildSpannedString {
            append("领取")
            color(R.color.colorAccent.colorResToColorId()) {
                append("${amount}贡献金")
            }
            append("30天福利 完成后方可再次选择 ")
        }
        // 根据贡献金显示按钮
        if (amount == BusinessConstans.level3) {
            // 只显示领取按钮
            binding.tvReceive.visibility = View.GONE
            binding.tvToHome.visibility = View.GONE
            binding.tvSingleReceive.visibility = View.VISIBLE
        } else {
            // 需要显示领取和消费按钮
            binding.tvReceive.visibility = View.VISIBLE
            binding.tvToHome.visibility = View.VISIBLE
            binding.tvSingleReceive.visibility = View.GONE
        }
        binding.tvReceive.setOnClickListener(this)
        binding.tvToHome.setOnClickListener(this)
        binding.ivClose.setOnClickListener(this)
        binding.tvSingleReceive.setOnClickListener(this)
    }

    override fun onClick(view: View?) {
        when (view?.id) {
            binding.tvReceive.id -> {
                dismiss()
                onReceive.invoke()
            }
            binding.tvSingleReceive.id -> {
                dismiss()
                onReceive.invoke()
            }
            binding.tvToHome.id -> {
                dismiss()
                onToHome.invoke()
            }
            binding.ivClose.id -> {
                dismiss()
            }
        }
    }

}